03. 纯函数和非纯函数
//纯函数
function square(x) {
return x + x;
}
function squareAll(items) { //不损坏原有的数据
return items.map(square);
}
//非纯函数
function square(x) {
updateXInDatabase(x); //这个业务逻辑不符合这函数的纯粹的意义
return x + x;
}
function squareAll(items) { //覆盖掉了原来的items
for (let i = 0; i < items.length; i++) {
items[i] = square(items[i]);
}
}